package com.ecommerce.controller;

import com.ecommerce.entity.Cart;
import com.ecommerce.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
@CrossOrigin(origins = "*")
public class CartController {
    // 注入CartService
    @Autowired
    private CartService cartService;

    // 获取购物车列表
    @GetMapping
    public ResponseEntity<List<Map<String, Object>>> getCartList(@RequestParam Long userId) {
        return ResponseEntity.ok(cartService.getCartList(userId));
    }

    // 添加商品到购物车
    @PostMapping("/add")
    public ResponseEntity<Cart> addToCart(@RequestParam Long userId, @RequestParam Long productId, @RequestParam Integer quantity) {
        return ResponseEntity.ok(cartService.addToCart(userId, productId, quantity));
    }

    // 更新购物车商品数量
    @PutMapping("/update")
    public ResponseEntity<Cart> updateCart(@RequestParam Long userId, @RequestParam Long productId, @RequestParam Integer quantity) {
        return ResponseEntity.ok(cartService.updateCart(userId, productId, quantity));
    }

    // 删除购物车商品
    @DeleteMapping("/remove")
    public ResponseEntity<Void> removeCartItem(@RequestParam Long userId, @RequestParam Long productId) {
        cartService.removeCartItem(userId, productId);
        return ResponseEntity.ok().build();
    }

    // 清空购物车
    @DeleteMapping("/clear")
    public ResponseEntity<Void> clearCart(@RequestParam Long userId) {
        cartService.clearCart(userId);
        return ResponseEntity.ok().build();
    }
}